#ifndef DATABASE_SESSION_H
#define DATABASE_SESSION_H

#include <stdio.h>
#include <iostream>
#include <QPointer>
#include <QtCore>
#include <QDebug>

#include <QSqlError>
#include <QSqlQuery>
#include <QSqlTableModel>
#include <QSqlQueryModel>
#include <QSqlField>
#include <QSqlIndex>
#include <QXmlStreamWriter>
#include "QmyDom.h"
#include <QApplication>







////////////////extern void my_thread_end(void);


enum outTypes { txtCSV , txtSQLITE, txtEXCEL };

QString encodeBase64( QString xml );
QString decodeBase64( QString xml );

QString allowName(const QString txt);

QByteArray utf_8config();
QString fo2pdfserialize( const QString xmlfo , const QString pdffile );

void cellxmlStringCall( const QString name , QXmlStreamWriter &writer  ,  QString dat );
QByteArray xmlsqlite2Dump( const  QString xmlfile , const  QString tablename );

QString StringStripslash( QString t );
QString StringtoXML( QString t );
bool GrepNummer ( QString data );
QByteArray xmlutf_8Query( const  QString query , const  QString connection_name );
QByteArray xmlexcel2008( const  QString query , const  QString connection_name );
QByteArray textQuery( const  QString query , const  QString connection_name , outTypes e = txtCSV );

bool Cache( const QString dirpath );
bool qt_unlink(QString fullFileName);
bool fwriteutf8(QString fullFileName, QString xml );


class DataBase_Session : public QObject
{
     Q_OBJECT
//
public:
 static DataBase_Session* instance();
 void append( const QString sheme );

  QMap<int,QDomElement> PipeList;

protected:
  void messagePrint( const QString msg , bool exits );
private:
    
 DataBase_Session();
 static QPointer<DataBase_Session> _self;
 int calling;
 uint sessionendTime;
 uint sessionTimeStart;
signals:
  void session_message(QString,bool);
  void connect(bool);
public slots:
    void close();
    

};






class ChildImport : public QXmlStreamReader
{
public:
ChildImport( QIODevice* device = 0 ) 
 :QXmlStreamReader( device )
{
  setNamespaceProcessing(false);
  /* only prefix append if exist */
}
/* copy all subelement from reader to writer out */
void copyDeep( QIODevice* device , QXmlStreamWriter &out  )
{
    setDevice(device);
    while (!atEnd()) {
         readNext();
         if ( isStartElement() ) {
             if (prefix().toString().isEmpty()) {
             out.writeStartElement(name().toString());
             } else {
             out.writeStartElement(prefix().toString()+":"+name().toString()); 
             }
             if (attributes().size() > 0) {
             out.writeAttributes(attributes());
             }      
         } else if (!isWhitespace()) {
             /* element having text or ??? */
             out.writeCharacters(text().toString());
             ////////out.writeEndElement();
             
         } else if (isComment()) {
            /* leave not import */
         } else if (isCDATA()) {
            /* take cdata */
             out.writeComment(text().toString());
         } else if ( isEndElement() ) {
             out.writeEndElement();
         }  else {
             //////////out.writeEndElement();
             QApplication::beep();
         }
         
   }
   const int erno = (int)error();
   if (erno != 0) {
       qWarning() << "### ChildImport::copyDeep error    ---------- " << error();
   } else {
      /////out.writeEndDocument(); 
   }
   device->close();
   
}

};













//
#endif // DATABASE_SESSION_H

